local function showMessageClean(str)
	if type(str) ~= "string" and type(str) ~= "number" then
		str = type(str)
	end
	ChatFrame1:AddMessage(str, 1, 1, 0.1)
end

local function showError(str)
	if type(str) ~= "string" and type(str) ~= "number" then
		str = type(str)
	end
	ChatFrame1:AddMessage("Underbidder-CLI: "..str, 1, 0.1, 0.1)
end

local function showMessage(str)
	if type(str) ~= "string" and type(str) ~= "number" then
		str = type(str)
	end
	showMessageClean("Underbidder-CLI: "..str)
end

if not AucAdvanced then showError("Underbidder-CLI: No AucAdvanced found.") return end
local Underbidder = AucAdvanced.GetModule("Util", "Underbidder")
if not Underbidder then showError("No Underbidder found.") return end
local lib = Underbidder

local oligarchs = {
	Ardisia = true;
	Vorbis = true;
	Erzhog = true;
	Gurbog = true;
	Lohriana = true;
}

local slashCmds = {
	"/underbidder";
	"/udb";
	"/ub";
}

function lib.GetOligarchs()
	return AucAdvanced.Settings.GetSetting("util.underbidder.oligarchs")
end

function lib.SetOligarchs(newOligarchs)
	if type(newOligarchs) == "table" then
		AucAdvanced.Settings.SetSetting("util.underbidder.oligarchs", newOligarchs)
	end
end

function lib.IsOligarch(name)
	if not name then return false end
	local oligarchs = lib.GetOligarchs()
	if type(oligarchs) == "table" then
		return oligarchs[name]
	else
		return false
	end
end

function lib.AddOligarch(name)
	if not name then return false end
	local oligarchs = lib.GetOligarchs()
	if type(oligarchs) == "table" then
		oligarchs[name] = true
		lib.SetOligarchs(oligarchs)
		return true
	else
		return false
	end
end

function lib.RemoveOligarch(name)
	if not name then return false end
	local oligarchs = lib.GetOligarchs()
	if type(oligarchs) == "table" then
		if oligarchs[name] then
			oligarchs[name] = nil
			lib.SetOligarchs(oligarchs)
		end
		return true
	else
		return false
	end
end

function lib.ListOligarch()
	if not name then return false end
	local oligarchs = lib.GetOligarchs()
	showMessage("Oligarchs: ")
	local listed = 0
	if type(oligarchs) == "table" then
		local curStr = ""
		for k,v in pairs(oligarchs) do
			listed = listed + 1
			if strlen(curStr) > 0 then curStr = curStr..", " end
			curStr = curStr..k
			if strlen(curStr) > 75 then
				showMessageClean(curStr)
				curStr = ""
			end
		end
		if strlen(curStr) > 0 then
			showMessage(curStr)
			curStr = ""
		end
	end
	if listed == 0 then
		showMessage("<none>")
	end
end

function lib.InitUnderbidderCLI()
	local cmdIndex = "UNDERBIDDERCLI"
	SlashCmdList[cmdIndex] = function(...) lib.UnderbidderCLI(...) end
	for k, v in pairs(slashCmds) do 
		setglobal("SLASH_"..cmdIndex..k, v)
	end
end

function lib.getWord(str)
	if type(str) ~= "string" then return "", "" end
	local iStart, iEnd = string.find(str, "%w+")
	if iStart then
		return string.sub(str, iStart, iEnd), string.sub(str, iEnd+1)
	else
		return "", ""
	end
end

local cli = {
	oligarch = {
		add = function(params)
				local oligarch = lib.getWord(params)
				if strlen(oligarch) > 0 then
					if lib.AddOligarch(oligarch) then
						showMessage("Added "..oligarch)
					end
					return true
				end
			end;
		remove = function(params)
				local oligarch = lib.getWord(params)
				if strlen(oligarch) > 0 then
					if lib.RemoveOligarch(oligarch) then
						showMessage("Removed "..oligarch)
					end
					return true
				end
			end;
		list = function(params)
				lib.ListOligarch()
				return true
			end;
	}
}
cli.oligarch.del = cli.oligarch.remove
cli.oligarch.show = cli.oligarch.list

function lib.UnderbidderCLI(baseCommand)
	local section, cmd, params = lib.getWord(baseCommand)
	if strlen(section) > 0 then
		cmd, params = lib.getWord(params)
	end
	if strlen(section) > 0 and strlen(cmd) > 0 then
		local commands = cli[section]
		if type(commands) == "table" then
			local func = commands[cmd]
			if type(func) == "function" then
				if func(params) then
					return true
				end
			end
		end
	end
	lib.UnderbidderCLIUsage()
end

function lib.UnderbidderCLIUsage()
	local usage = {
		" usage: /underbidder <section> <command> [parameter]";
		"";
	}
	for k, v in pairs(usage) do showMessageClean(v) end
	local sections
	for k,v in pairs(cli) do
		if sections then 
			sections = sections..", "..k
		else
			sections = sections..k
		end
	end
	showMessageClean(" sections: "..sections)
	local curStr = nil
	for k,v in pairs(cli) do
		if type(v) == "table" then
			showMessageClean(" "..k.." commands: ")
			curStr = nil
			for l,w in pairs(v) do
				if curStr then
					curStr = curStr..", "..l
				else
					curStr = l
				end
				if strlen(curStr) > 75 then
					showMessageClean("  "..curStr)
					curStr = nil
				end
			end
			if strlen(curStr) > 0 then
				showMessageClean("  "..curStr)
				curStr = nil
			end
		end
	end
end

lib.InitUnderbidderCLI()